package net.ssta.util.bill;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;

import net.ssta.common.Constants;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.Record;

public class UserOrder {
	private static Logger log = Logger.getLogger(UserOrder.class.getClass()); 
	public String orderId;
	public int moneyNum;
	public String userId;
	public Date orderTime;
	public String isFinish;
	
	public UserOrder(){
		super();
	}
	public UserOrder(String _orderId)throws SQLException{
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	Record r=new Record();
	    	r.setConnection(conn);
	    	r.setEntityName("T_USER_ORDER");
	    	r.addData("ORDERID",_orderId);
	    	r=r.findByPrimaryKey();
	    	if(r==null){
	    		r=new Record();
	    	}
	    	this.moneyNum=r.getInt("MONEYNUM");
	    	this.orderId=_orderId;
	    	this.userId=r.getString("USERID");
	    	this.orderTime=r.getDate("ORDERTIME");
	    	this.isFinish=r.getString("ISFINISH");
	    	
		} catch (SQLException e) {
			log.error("查找订单出错："+e); 
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	/**
	 * 是否完成
	 * @return
	 */
	public boolean isFinish(){
		if("1".equals(this.isFinish)){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @throws SQLException
	 */
	public  void createOrder()throws SQLException{
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	Record r=new Record();
	    	r.setConnection(conn);
	    	r.setEntityName("T_USER_ORDER");
	    	r.addData("ORDERID",orderId);
	    	r.addData("USERID",userId);
 	    	r.addData("MONEYNUM",moneyNum);
	    	r.addData("ISFINISH","0");
	    	r.addData("ORDERTIME",new Date());
	        r.create();
		} catch (SQLException e) {
			log.error("存储订单出错："+e); 
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	/**
	 * 处理订单
	 * @throws SQLException
	 * @throws Exception
	 */
	public  void finishOrder()throws SQLException,Exception{
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		conn.setAutoCommit(false);
    		
    		UserPoint point=new UserPoint(this.userId);
    		point.rechargeAlipay(this.moneyNum, conn);
    		
	    	Record r=new Record();
	    	r.setConnection(conn);
	    	r.setEntityName("T_USER_ORDER");
	    	r.addData("ORDERID",this.orderId);
	    	r.setPrimaryKey("ORDERID");
	    	r.addData("ISFINISH","1");
	    	r.update();
	        
	        conn.commit();
		} catch (SQLException e) {
			log.error("处理订单出错："+e); 
			throw e;
		}catch (Exception e) {
			log.error("处理订单出错："+e); 
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}
	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	/**
	 * @return the moneyNum
	 */
	public int getMoneyNum() {
		return moneyNum;
	}
	/**
	 * @param moneyNum the moneyNum to set
	 */
	public void setMoneyNum(int moneyNum) {
		this.moneyNum = moneyNum;
	}
	/**
	 * @param moneyNum the moneyNum to set
	 */
	public void setMoneyNum(String moneyNum) {
		this.moneyNum = Integer.parseInt(moneyNum);
	}
	/**
	 * @return the userId
	 */
	public String getUserId() {
		return userId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}
	/**
	 * @return the orderTime
	 */
	public Date getOrderTime() {
		return orderTime;
	}
	/**
	 * @param orderTime the orderTime to set
	 */
	public void setOrderTime(Date orderTime) {
		this.orderTime = orderTime;
	}
	/**
	 * @return the isFinish
	 */
	public String getIsFinish() {
		return isFinish;
	}
	/**
	 * @param isFinish the isFinish to set
	 */
	public void setIsFinish(String isFinish) {
		this.isFinish = isFinish;
	}
	
	
	

}
